package com.sky.web;

import com.sky.service.CartService;
import com.sky.vo.CartItemVo;
import com.sky.vo.CartVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class CartWebController {

    @Resource
    private CartService cartService;

    /**
     * 购物车列表查询
     **/
    @GetMapping("/cartList.html")
    public Object getCartList(Model model) {
        CartVo cartVo = cartService.getUserCartList();
        model.addAttribute("cartVo", cartVo);
        return "cartList";
    }

    /**
     * 商品添加到购物车
     **/
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes redirectAttributes) throws Exception{
        cartService.addToCart(skuId, num);
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.skymall.com/addToCartSuccess.html";
    }

    /**
     * 查询添加到购物车的商品
     **/
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId, Model model) {
        CartItemVo cartItemVo = cartService.getCartItem(skuId);
        model.addAttribute("cartItem", cartItemVo);
        return "success";
    }

    /**
     * 购物车选中
     **/
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId, @RequestParam("checked") Integer checked) {
        cartService.checkItem(skuId, checked);
        return "redirect:http://cart.skymall.com/cartList.html";
    }

    /**
     * 购物车商品数量修改
     **/
    @GetMapping("/changeItemCount")
    public String changeItemCount(@RequestParam("skuId") Long skuId, @RequestParam("count") Integer count) {
        cartService.changeItemCount(skuId, count);
        return "redirect:http://cart.skymall.com/cartList.html";
    }

    /**
     * 删除购物车中的一类商品
     **/
    @GetMapping("/deleteSkuItem")
    public String deleteSkuItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteSkuItem(skuId);
        return "redirect:http://cart.skymall.com/cartList.html";
    }
}
